<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:u="livcos.org/ns/cosmos/update"
  exclude-result-prefixes="pm u doc">
  
  <xsl:param name="obj"/>

  <xsl:template match="/">
    <u:context ref="{$obj}" scope="content">
      <xsl:apply-templates select="document(concat($obj, '.content'))/*"/>
    </u:context>
  </xsl:template>
  
  <xsl:template match="*">
    <xsl:apply-templates select="*"/>
  </xsl:template>
  
	<xsl:template match="doc:paragraph/doc:*[@id]">
		<u:insert select=".//*[@id = '{../@id}']" position="before">
			<u:move-from select="*[@id = '{@id}']"/>
		</u:insert>
	</xsl:template>
	
	<xsl:template match="doc:paragraph/doc:link" priority="2">
		<xsl:apply-templates select="*"/>
	</xsl:template>
	
	<xsl:template match="doc:cell[not(parent::doc:row)]">
		<u:insert select=".//*[@id = '{@id}']" position="after">
			<table id="{{new-id()}}" xmlns="livcos.org/ns/doc">
				<body>
					<row id="{{new-id()}}">
						<u:move-from select="."/>
					</row>
				</body>
			</table>
		</u:insert>
	</xsl:template>
	
	<xsl:template match="doc:row">
    <u:insert select=".//*[@id = '{@id}']" position="after">
      <table id="{{new-id()}}" xmlns="livcos.org/ns/doc">
        <body>
          <u:move-from select="."/>
        </body>
      </table>
    </u:insert>
  </xsl:template>
  
  <xsl:template match="doc:row[parent::doc:*/parent::doc:table]" priority="2">
  </xsl:template>
  
  <xsl:template match="doc:row[parent::doc:table]" priority="3">
    <u:insert select=".//*[@id = '{@id}']/../doc:body" create-path="yes">
      <u:namespace prefix="doc" uri="livcos.org/ns/doc"/>
      <u:move-from select="../*[@id = '{@id}']"/>
    </u:insert>
  </xsl:template>
  
  <xsl:template match="doc:item">
    <u:insert select=".//*[@id = '{@id}']" position="after">
      <list id="{{new-id()}}" xmlns="livcos.org/ns/doc">
        <u:move-from select="."/>
      </list>
    </u:insert>
  </xsl:template>
  
  <xsl:template match="doc:list/*[@id]">
    <u:insert select=".//*[@id = '{@id}']" position="after">
      <item id="{{new-id()}}" xmlns="livcos.org/ns/doc">
        <u:move-from select="."/>
      </item>
    </u:insert>
  </xsl:template>
  
  <xsl:template match="doc:list/doc:item" priority="2">
  </xsl:template>
  
  <xsl:template match="pm:val[count(../*) > 1][1]">
    <u:update select=".//*[@id = '{@id}']/..">
      <pm:operand id="{{new-id()}}" type="and">
        <xsl:copy-of select="../*"/>
      </pm:operand>
    </u:update>
  </xsl:template>
  
  <xsl:template match="pm:operand">
  </xsl:template>
  
</xsl:stylesheet>
